package kos.robert.core;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Transparency;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

public class Look extends Core implements MouseMotionListener, KeyListener {
	public static void main(String[] args) {
		new Look().run();
	}

	private Image bg;
	BufferedImage bufImg;
	private Robot robot;
	private Point mouse;
	private Point center;
	private Point image;
	private boolean centering;

	// init
	public void init() {
		super.init();
		mouse = new Point();
		center = new Point();
		image = new Point();
		centering = false;

		try {
			robot = new Robot();
			recenterMouse();
			mouse.x = center.x;
			mouse.y = center.y;
		} catch (Exception e) {
			e.printStackTrace();
		}

		Window window = screen.getFullScreenWindow();
		window.addMouseMotionListener(this);
		window.addKeyListener(this);
		bg = new ImageIcon("resources/graphics/back.png").getImage();
		bufImg = screen.createCompatibleImage(bg.getWidth(null), bg.getHeight(null), Transparency.BITMASK);
		Graphics2D g = (Graphics2D) bufImg.getGraphics();
		g.drawImage(bg, 0, 0, null);
	}

	// draw
	@Override
	public synchronized void draw(Graphics2D g) {
		int width = screen.getWidth();
		int height = screen.getHeight();

		image.x %= width;
		image.y %= height;
		if (image.x < 0) {
			image.x += width;
		}
		if (image.y < 0) {
			image.y += height;
		}
		int x = image.x;
		int y = image.y;
//		g.drawImage(bg, image.x, image.y, null);
		g.drawImage(bufImg, image.x, image.y, null);
		g.drawImage(bufImg, x - width, y, null);
		g.drawImage(bufImg, x, y - height, null);
		g.drawImage(bufImg, x - width, y - height, null);
	}

	// recenter the mouse using robot
	private synchronized void recenterMouse() {
		Window window = screen.getFullScreenWindow();
		if (robot != null && window.isShowing()) {
			center.x = window.getWidth() / 2;
			center.y = window.getHeight() / 2;
			SwingUtilities.convertPointToScreen(center, window);
			centering = true;
			robot.mouseMove(center.x, center.y);
		}
	}

	// mousemotion listener method
	@Override
	public void mouseDragged(MouseEvent e) {
		mouseMoved(e);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		if (centering && mouse.x == e.getX() && mouse.y == e.getY()) {
//			centering = false;
		} else {
			int dx = e.getX() - mouse.x;
			int dy = e.getY() - mouse.y;
			image.x += dx;
			image.y += dy;
			centering = true;
//			recenterMouse();
		}

		mouse.x = e.getX();
		mouse.y = e.getY();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			stop();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// Nothing to do
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// Nothing to do
	}
}
